<?php 
class Crud_work_model extends grocery_CRUD_Model {
    function get_list(){
        $pj_id = $this->uri->segment(3);
        $sql = "SELECT * FROM tblworks WHERE projectid='$pj_id'";
        $arr = $this->db->query($sql)->result();   
        return $this->works_multi_level_result($arr);
        
    }
    function db_insert($post_array) {
        $post_array['projectid'] = $this->uri->segment(3);
        $post_array['parentid']= $this->uri->segment(4);;
        parent::db_insert($post_array);
        
    }
    function works_multi_level($arrData, $parentid = 0, $level = 0, &$result) {
        if (count($arrData) > 0) {
            foreach ($arrData as $key => $val) {
                if ($parentid == $val->parentid) {
                    $val->level = $level;
                    $result[] = $val;
                    $_parentid = $val->id;
                    unset($arrData[$key]);
                    $this->works_multi_level($arrData, $_parentid, $level + 1, $result);
                }
            }
        }
 }
    function works_multi_level_result($arrData,$selected_id=null){
        $this->works_multi_level($arrData, 0, 0, $result);
        for($i=0;$i<count($arrData);$i++){
            
        }
        foreach ($result as $key => $val) {
            $numRepeat = $val->level;
            $char = '';
            if ($numRepeat > 0) {
                $char .= str_repeat('&rarr;', $numRepeat);
            }
            $val->name = $char.'&nbsp;'.$val->name;
            
        }
        return $result;
 }
}
?>
